home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: more problems with qsort
- Date: 28 Feb 1996 15:59:36 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Feb28085936@qcd.lanl.gov>
- References: <177399702S86.JW1675A@american.edu> <4gt9i7INN76i@keats.ugrad.cs.ubc.ca>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: c2a192@ugrad.cs.ubc.ca's message of 26 Feb 1996 13:43:35 -0800
-
- In article <4gt9i7INN76i@keats.ugrad.cs.ubc.ca>
- c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) writes:
- <snip>
- KK: >I'm doing some file manipulation and between steps A and B, I
- need to sort.
- KK: >For various reasons, I don't want to popen() to the sort
- utility--I want to
- KK: >use qsort. Here's what I'm doing:
- KK: >
- KK: > get number of lines in the file
- KK: > (char**)malloc with enough room for all lines
- KK: > for each line in the file {
- KK: > (char*)malloc(90) /* 90 is enough room for each line in file */
- KK: > copy each line into the newly malloc()ed space
- KK: > point a (char**) to the newly malloc()ed space
- KK: > }
- KK: >
- KK: >so now I should have "lines" number of pointers to pointers to char,
- KK: >each one pointing to 90 bytes containing a line in the file.
- KK: >
- KK: >So, I call qsort(array[0], lines, 90, compare)
- KK: >where compare is my comparison function -- prepared as discussed
- KK: >in the FAQ. Now all I get are core dumps during the call to qsort().
- KK: >:-)
- KK:
- KK: You need a pointer to the first element of the array, not the first element
- KK: itself. You should be calling it qsort(array, ... );
- KK:
-
- This is the second wrong answer, though I don't blame them. People who
- do not post code deserve this. The design, I believe, is fundamentally
- flawed and such patches of removing [0] will not solve
- it.
-
- Look at the 90 in the call to malloc. Now look at the statement above
- about `(char**)malloc'. Try to mentally complete that to compilable
- code ... does something strike someone as unusual :-)?
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-